/**
 * @filename:ParameterController 2023-4-17
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.Parameter;
import com.wonder.admin.service.ParameterService;
import com.wonder.admin.request.ParameterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.utils.DBUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 参数配置API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    2023-4-17
 *
 */
@Api(tags = "参数配置")
@RestController
@RequestMapping("/admin/Parameter")
public class ParameterController {
	
	@Autowired
	private ParameterService parameterService;
	
	@Autowired
	private DBUtils dbUtils;
	
	@ApiOperation(value = "参数配置列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody ParameterRequest parameterRequest) {
		Page<Parameter> page = new Page<Parameter>(parameterRequest.getPage(), parameterRequest.getLimit());
		IPage<Parameter> list = parameterService.selectList(parameterRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
        Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("languages",dbUtils.getDropDownList("select code,name from sys_language"));
        retMap.put("sql_field_data",smallTable);
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 获取sql语句定义的字段信息
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/getFieldList")
	@ApiOperation(value = "获取sql语句定义的字段信息", notes = "作者：Wonder")
	public AjaxResult<Map<String,Object>> getFieldList(){
	    Map<String,Object> smallTable=new HashMap<>();
        smallTable.put("languages",dbUtils.getDropDownList("select code,name from sys_language"));
        return AjaxResult.success(smallTable);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Parameter> updateExt(@RequestBody Parameter entity){
		if (null!=entity) {
			boolean rsg = parameterService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Parameter> add(@RequestBody Parameter entity){

		if (null!=entity) {
		    
		    String validate=parameterService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = parameterService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 复制添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/clone")
	@ApiOperation(value = "复制添加", notes = "作者：Wonder")
	public AjaxResult<Parameter> clone(@RequestBody Parameter entity){

		if (null!=entity) {
		    
		    String validate=parameterService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = parameterService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取参数配置对象", notes = "作者：Wonder")
	public AjaxResult<Parameter> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		 Parameter parameter=parameterService.getById(id);
		
		if (null!=parameter ) {
			 return AjaxResult.success(parameter);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Parameter> update(@RequestBody Parameter entity){
		if (null!=entity) {
		    String validate=parameterService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = parameterService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Parameter> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = parameterService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取参数配置对象明细", notes = "作者：Wonder")
	public AjaxResult<Parameter> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		Parameter parameter=parameterService.getById(id);
		if (null!=parameter ) {	
			 return AjaxResult.success(parameter);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	
}